package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.SelectBean;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.TrainListBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditApplyTitleBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.map.SelectMapActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiPostActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.ui.select.SelectTrainActivity;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;
import java.util.ArrayList;

public class ApplyTrainPurchaseActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;
    private String msg;
    private TrainListBean trainListBean;
    private String contracts;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "培训采购");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TRAIN_PURCHASE)
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("theCategory", trainListBean.getTheCategory())
                        .params("theTheme", trainListBean.getTheTheme())
                        .params("keywords", setParam(2))
                        .params("trainees", trainListBean.getTrainees())
                        .params("thedateStart", setParam(4))
                        .params("thedateEnd", setParam(5))
                        .params("theplace", setParam(6))
                        .params("comName", setParam(7))
                        .params("contacts", contracts)
                        .params("contactsName", setParam(8))
                        .params("phone", setParam(9))
                        .params("handset", setParam(10))
                        .params("qq", setParam(11))
                        .params("otherDemand", setParam(12))
                        .params("budgetedExpense", trainListBean.getBudgetedExpense())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.REFRESH);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParams() {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).isNeed()) {
                if (isParamBlank(i)) {
                    msg = "请输入" + slimAdapterEx.getDataItem(i).getTitle().replace("*", "");
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 6:
                startActivityForResult(SelectMapActivity.class, null, Config.SELECT_MAP);
                break;
            case 8:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;
        startActivityForResult(SelectTrainActivity.class, null, Config.SELECT_TRAIN);

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Config.SELECT_TRAIN) {
            if (resultCode == Config.SUCCESS) {
                trainListBean = (TrainListBean) data.getSerializableExtra(Config.BEAN);
                contracts = AppConstent.user.getGuid();
                datas.add(new SlimEditHintBean("*培训类别", trainListBean.getTheCategory(), 0).setNeed(true));
                datas.add(new SlimEditHintBean("*培训主题", trainListBean.getTheTheme(), 1).setNeed(true));
                datas.add(new SlimEditEnterBean("*关键词", "", 2, "请输入").setNeed(true));
                datas.add(new SlimEditHintBean("*培训对象", trainListBean.getTrainees(), 3).setNeed(true));
                datas.add(new SlimEditTimeBean("*培训开始时间", trainListBean.getThedateStart(), 4, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
                datas.add(new SlimEditTimeBean("*培训结束时间", trainListBean.getThedateEnd(), 5, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
                datas.add(new SlimEditEnterBean("*培训地点", "", 6, true, "请输入").setNeed(true));
                datas.add(new SlimEditHintBean("*公司名称", AppConstent.user.getCompany(),7).setNeed(true));
                datas.add(new SlimEditEnterBean("*联系人", AppConstent.user.getUsername(), 8, true, "请输入").setNeed(true));
                datas.add(new SlimEditEnterBean("电话", "", 9, "请输入"));
                datas.add(new SlimEditEnterBean("*手机", AppConstent.user.getUname(), 10, "请输入").setNeed(true));
                datas.add(new SlimEditEnterBean("QQ", "", 11, "请输入"));
                datas.add(new SlimEditEnterBean("其它要求", "", 12, "请输入"));
                slimAdapterEx.notifyItemRangeInserted(0, 13);
            } else {
                finish();
            }
        } else if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_MAP) {
            String map = data.getStringExtra(Config.MAP_RESULT_STRING);
            slimAdapterEx.getDataItem(6).setContent(map);
            slimAdapterEx.notifyItemChanged(6);
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            contracts = data.getStringExtra(Config.GUID);
            slimAdapterEx.getDataItem(8).setContent(content);
            slimAdapterEx.notifyItemChanged(8);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
